﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Text;

public partial class Agency_LimitPrice : System.Web.UI.Page
{
    string projectid;
    int cun1, cun2;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            projectid = SysFun.IsSQL(Request.QueryString["projectid"]);
            if (!IsPostBack)
            {
                LoadData();
            }
            if (Request.Form["subSavePrice"] == "保存限价")
            {
                SavePrice();
            }
            if (Request.Form["subLimit"] == "将招标药品导入限价表")
            {
                LeadIn();
            }
            showGridView();
        }
    }

    private void LeadIn()
    {
        Db db = new Db();
        string _strSql = "insert into limitpricetable (projectid,goodsid) select " + projectid + ",goodsid from dbo.v_product_goods where goodsid in (select goodsid from project_bid where projectid=" + projectid + " and goodsid not in (select goodsid from limitpricetable where projectid=" + projectid + ")) ";
        int _intRowCount = db.Command(_strSql);
        db.Close();
        Response.Write("<script>alert('导入成功（记录条数：" + _intRowCount.ToString() + "）！！！');</script>");
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "limitpricetable", _strSql, "../Agency/LimitPrice.aspx");

    }
    private void LoadData()
    {

        string sql;
        
        Db db = new Db();
        System.Data.IDataReader dr;
        sql = "select a.cun as cun1,b.cun as cun2 from (select count(goodsid) as cun from bid_project_product,goods where projectid=" + projectid +
              " and bid_project_product.productid=goods.productid And Checkstatus=1) a,(" +
              " select count(goodsid) as cun from LimitPriceTable where projectid=" + projectid + ") b";
        dr = db.DataReader(sql);
        while (dr.Read())
        {
            cun1 = SysFun.ToInt(dr["cun1"].ToString());
            cun2 = SysFun.ToInt(dr["cun2"].ToString());

        }
        dr.Close();
        dr.Dispose();
        db.Close();
        if (cun2 == 0)
        {
            Label1.Text = "还没有选择限价商品，请点击按钮";
        }
        else
        {
            if (cun1 > cun2 && cun2 != 0)
            {
                Label1.Text = "还有商品没有选入限价表,请点击按钮";
            }
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Text = "<input type=text name='txtprice" + e.Row.Cells[3].Text + e.Row.Cells[7].Text + "' value=\"" + SysFun.ToDec(e.Row.Cells[8].Text) + "\" /> ";
            e.Row.Cells[1].Text = "<textarea style='height:50'  name='txtMemo4" + e.Row.Cells[3].Text + e.Row.Cells[7].Text + "'   >" + e.Row.Cells[10].Text.Trim() + "</textarea > ";
            e.Row.Cells[2].Text = "<textarea  style='height:50' name='txtMemo6" + e.Row.Cells[3].Text + e.Row.Cells[7].Text + "'   >" + e.Row.Cells[11].Text.Trim() + "</textarea > ";
        }
        e.Row.Cells[8].Visible = false;
        e.Row.Cells[10].Visible = false;
        e.Row.Cells[11].Visible = false;
       
    }
    protected void SavePrice()
    {
        ErrorMsg.Text = "";
        string s="";
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                string a = Request.Form["txtprice" + row.Cells[3].Text + row.Cells[7].Text];
                string _strMemo4 = SysFun.ToTrim(Request.Form["txtMemo4" + row.Cells[3].Text + row.Cells[7].Text]);
                string _strMemo6 = SysFun.ToTrim(Request.Form["txtMemo6" + row.Cells[3].Text + row.Cells[7].Text]);
                if (!SysFun.IsNumeric(a))
                {
                    
                    ErrorMsg.Text += row.Cells[3].Text + "限价输入应该为数字！<br>";
                    ErrorMsg.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    if (Convert.ToDecimal(a) < 0 || Convert.ToDecimal(a) > 99999999)
                    {
                        ErrorMsg.Text += row.Cells[3].Text + "限价有误<br>";
                        ErrorMsg.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        if (SysFun.ToDec(a).ToString("N4") != row.Cells[8].Text)
                        {
                            //s += "@" + "update LimitPriceTable set referprice=" + SysFun.ToDec(a) + " where goodsid=" + SysFun.ToInt(row.Cells[7].Text) + " and projectid=" + projectid;
                            s += "@" + "Update LPNew Set LimitPrice=" + SysFun.ToDec(a).ToString("N4").Replace(",","") + ",memo5=getdate(),memo4='" + _strMemo4 + "',memo6='" + _strMemo6 + "' Where ProductCode='" + row.Cells[3].Text + "' And projectid=" + projectid + " And QualityLevels='" + row.Cells[7].Text + "'";
                        }
                    }
                }
            }
        }
        Db db = new Db();
        if (s.Length > 0)
        {
            db.ExecuteTransaction(s.Substring(1).Split(new char[] { '@' }), 30);
        }
        db.Close();
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "LPNew", s, "../Agency/LimitPrice.aspx");

    }

    public void showGridView()
    {

        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "LPNew";                                               //要查询的表明
        dbPage.SelectFieldGroup = "ProductCode,Bidtablename,bidtabletype,bidtableoutlookc,convert(numeric(20,4),LimitPrice),QualityLevels,convert(datetime,memo5 ),memo4,memo6 ";
        dbPage.Key = "ProductCode,convert(numeric(20,4),LimitPrice) as LimitPrice,QualityLevels";//表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "ProductCode,Bidtablename,bidtabletype,bidtableoutlookc,convert(numeric(20,4),LimitPrice) as LimitPrice,QualityLevels,convert(datetime,memo5 ) as memo5,memo4,memo6";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 30;                                                    //没有显示记录条数   
        dbPage.OrderBy = "ProductCode,QualityLevels";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReaderGroup(60);
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
    }
    public string fctConditions()
    {
        string _strWhere = "ProjectId=" + projectid;
        //品目号
        string _strProductCode = txtProductCode.Text.Trim();
        if (_strProductCode != "")
        {
            _strWhere += " And ProductCode like '%" + _strProductCode + "%' ";
        }
        //质量层次
        string _strQualityLevels = lstQualityLevels.SelectedItem.Text;
        if (_strQualityLevels != "全部")
        {
            _strWhere += " And  QualityLevels like '%" + _strQualityLevels + "%' ";
        }
        //限价是否更改
        string _strMemo4 = lstMemo4.SelectedValue;
        switch (_strMemo4)
        {
            case "1":
                
                break;
            case "2":
                _strWhere += " And  Memo4 is not null ";
                break;
            case "3":
                _strWhere += " And  Memo4 is  null "; 
                break;
        }
        
        //招标通用名
        string _strBidtableName = txtBidtableName.Text.Trim();
        if (_strBidtableName != "")
        {
            _strWhere += " And  BidtableName like '%" + _strBidtableName + "%' ";
        }
        //招标剂型
        string _strBidtableType = txtBidtableType.Text.Trim();
        if (_strBidtableType != "")
        {
            _strWhere += "  And  BidtableType like '%" + _strBidtableType + "%' ";
        }
        //招标规格
        string _strBidtableOutlookc = txtBidtableOutlookc.Text.Trim();
        if (_strBidtableOutlookc != "")
        {
            _strWhere += "  And  BidtableOutlookc like '%" + _strBidtableOutlookc + "%' ";
        }
        return _strWhere;
     
    }
}
